/*
 * 文件名:MaterialServiceImpl.java
 * 创建时间:2010-10-01
 * 版本:2.0
 * 版权所有:杨涛 walle1027@gmail.com
 */
package com.yangtao.wms.master.material.service.impl;

import com.yangtao.framework.hibernate.Selector;
import com.yangtao.framework.service.BaseService;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.wms.dao.CustomerSettingDao;
import com.yangtao.wms.dao.MaterialDao;
import com.yangtao.wms.dao.MaterialDetailDao;
import com.yangtao.wms.dao.MaterialGroupDao;
import com.yangtao.wms.domain.CustomerSetting;
import com.yangtao.wms.domain.Material;
import com.yangtao.wms.domain.MaterialDetail;
import com.yangtao.wms.domain.MaterialGroup;
import com.yangtao.wms.master.material.service.MaterialService;
import com.yangtao.wms.master.material.web.MaterialModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 代码生成器 v-2.0
 * @version 2.0
 */
@Service()
public class MaterialServiceImpl extends BaseService implements MaterialService {

	@Autowired
	protected MaterialDao materialDao;
	@Autowired
	protected MaterialDetailDao materialDetailDao;
	@Autowired
	protected CustomerSettingDao customerSettingDao;
	@Autowired
	protected MaterialGroupDao materialGroupDao;

	/**
	 * 查询记录，并且分页
	 *
	 * @param model 页面参数
	 */
	@Override
	public MaterialModel list(MaterialModel model) {
		Selector selector = new Selector();
		selector.add(".materialDetail");
		selector.add(".customerSetting");
		selector.add(".materialGroup");
		materialDao.findPageByCondition(selector, model.getPage(), model.getCondition());
		return model;
	}

	/**
	 * 取得唯一记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public MaterialModel loadMaterial(MaterialModel model) {
		if (StringHelper.isNotEmpty(model.getMaterial().getId())) {
			Selector selector = new Selector();
			selector.add(".materialDetail");
			selector.add(".customerSetting");
			selector.add(".materialGroup");
			model.setMaterial(materialDao.loadForReview(model.getMaterial().getId(), selector));
		}
		return model;
	}

	/**
	 * 新增记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public MaterialModel addMaterial(MaterialModel model) {
		//VO
		Material materialVO = model.getMaterial();
		//PO
		Material materialPO = new Material();
		mapper.map(materialVO, materialPO);
		//构造关联从对象
		//构造关联对象
		MaterialDetail materialDetailVO = materialVO.getMaterialDetail();
		MaterialDetail materialDetailPO = new MaterialDetail();
		mapper.map(materialDetailVO, materialDetailPO);
		//设置额外属性
		setMaterialDetailProperties(materialDetailPO, model);
		//构造关联对象关联关系
		materialDetailPO.setMaterial(materialPO);
		materialPO.setMaterialDetail(materialDetailPO);
		//构造关联对象
		CustomerSetting customerSettingVO = materialVO.getCustomerSetting();
		CustomerSetting customerSettingPO = new CustomerSetting();
		mapper.map(customerSettingVO, customerSettingPO);
		//设置额外属性
		setCustomerSettingProperties(customerSettingPO, model);
		//构造关联对象关联关系
		customerSettingPO.setMaterial(materialPO);
		materialPO.setCustomerSetting(customerSettingPO);
		//从model中取出关联子对象列表
		List<MaterialGroup> materialGroupVO = materialVO.getMaterialGroup();
		if (materialGroupVO != null && materialGroupVO.size() > 0) {
			List<MaterialGroup> materialGroupPO = new ArrayList<MaterialGroup>();
			for (MaterialGroup voItem : materialGroupVO) {
				if (voItem != null) {
					MaterialGroup poItem = new MaterialGroup();
					mapper.map(voItem, poItem);
					//设置额外属性
					setMaterialGroupProperties(poItem, model);
					poItem.setMaterial(materialPO);
					materialGroupPO.add(poItem);
				}
			}
			materialPO.setMaterialGroup(materialGroupPO);
		}
		//保存对象
		materialDao.save(materialPO);
		//增加保存成功的消息
		model.addInfo("add.success");
		return model;
	}

	/**
	 * 更新记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public MaterialModel updateMaterial(MaterialModel model) {
		//VO
		Material materialVO = model.getMaterial();
		//主键
		String materialId = materialVO.getId();
		//PO
		Material materialPO = materialDao.load(materialId);
		//复制页面属性
		mapper.map(materialVO, materialPO);
		//TODO 增加其他属性
		//TODO 更新关联对象
		//构造关联从对象
		//构造关联对象，一对一的情况下，假定它不会更新关联关系
		String materialDetailId = materialVO.getMaterialDetail() == null ? null : materialVO.getMaterialDetail().getId();
		MaterialDetail materialDetailPO = materialDetailDao.load(materialDetailId);
		if (materialDetailPO != null) {
			//根据页面展示项 copy字段，此处要留有扩展点
			mapper.map(materialVO.getMaterialDetail(), materialDetailPO);
			//增加其他属性
			setMaterialDetailProperties(materialDetailPO, model);
			//构造关联对象关联关系
			materialDetailPO.setMaterial(materialPO);
		}

		//构造关联对象，一对一的情况下，假定它不会更新关联关系
		String customerSettingId = materialVO.getCustomerSetting() == null ? null : materialVO.getCustomerSetting().getId();
		CustomerSetting customerSettingPO = customerSettingDao.load(customerSettingId);
		if (customerSettingPO != null) {
			//根据页面展示项 copy字段，此处要留有扩展点
			mapper.map(materialVO.getCustomerSetting(), customerSettingPO);
			//增加其他属性
			setCustomerSettingProperties(customerSettingPO, model);
			//构造关联对象关联关系
			customerSettingPO.setMaterial(materialPO);
		}

		//从model中取出关联子对象列表
		//先遍历PO
		List<MaterialGroup> materialGroupPO = materialPO.getMaterialGroup();
		List<MaterialGroup> materialGroupVO = materialVO.getMaterialGroup();
		if (materialGroupPO == null) {
			materialGroupPO = new ArrayList<MaterialGroup>();
			materialPO.setMaterialGroup(materialGroupPO);
		}
		if (materialGroupVO == null) {
			materialGroupVO = new ArrayList<MaterialGroup>();
		}
		List<MaterialGroup> _materialGroupVO = new LinkedList<MaterialGroup>();
		_materialGroupVO.addAll(materialGroupVO);
		//更新或者删除
		for (Iterator<MaterialGroup> i = materialGroupPO.iterator(); i.hasNext(); ) {
			MaterialGroup poItem = i.next();
			boolean isDeletedItem = true;
			for (Iterator<MaterialGroup> j = _materialGroupVO.iterator(); j.hasNext(); ) {
				MaterialGroup voItem = j.next();
				if (poItem.getId().equals(voItem.getId())) {
					//更新明细
					mapper.map(voItem, poItem);
					isDeletedItem = false;
					j.remove();
				}
			}
			if (isDeletedItem) {
				i.remove();
				materialGroupDao.delete(poItem);
			}
		}
		//新增
		if (_materialGroupVO.size() > 0) {
			for (MaterialGroup voItem : _materialGroupVO) {
				MaterialGroup poItem = new MaterialGroup();
				mapper.map(voItem, poItem);
				//设置额外属性
				setMaterialGroupProperties(poItem, model);
				//TODO 根据级联设置，来决定操作
				poItem.setMaterial(materialPO);
				materialGroupPO.add(poItem);
			}
		}
		//增加保存成功的消息
		model.addInfo("update.success");
		return model;
	}

	/**
	 * 删除记录
	 *
	 * @param model 页面参数
	 */
	@Override
	public MaterialModel deleteMaterial(MaterialModel model) {
		materialDao.delete(model.getMaterial().getId());
		model.addInfo("delete.success");
		return model;
	}

	/**
	 * 设置关联对象:物料详细信息表 的其他属性
	 * 页面上无法传递过来的属性可以在此增加
	 * 子类重写此方法可以在新增和修改的时候为关联实体增加额外的属性
	 *
	 * @param materialDetail 关联对象
	 * @param model          页面参数类
	 */
	protected void setMaterialDetailProperties(final MaterialDetail materialDetail, final MaterialModel model) {
	}

	/**
	 * 设置关联对象:客户设置信息表 的其他属性
	 * 页面上无法传递过来的属性可以在此增加
	 * 子类重写此方法可以在新增和修改的时候为关联实体增加额外的属性
	 *
	 * @param customerSetting 关联对象
	 * @param model           页面参数类
	 */
	protected void setCustomerSettingProperties(final CustomerSetting customerSetting, final MaterialModel model) {
	}

	/**
	 * 设置关联对象:物料组表 的其他属性
	 * 页面上无法传递过来的属性可以在此增加
	 * 子类重写此方法可以在新增和修改的时候为关联实体增加额外的属性
	 *
	 * @param materialGroup 关联对象
	 * @param model         页面参数类
	 */
	protected void setMaterialGroupProperties(final MaterialGroup materialGroup, final MaterialModel model) {
	}
}
